﻿package fwk.form.elements{
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	public class QuestionRadioCheck extends AbstractQuestion 
	{
		private var _containerReponses:MovieClip;
		private var _reponse:AbstractReponse;
		
		private var _itemFocus:ReponseRadioCheck;
		
		public function QuestionRadioCheck(mc:MovieClip = null) 
		{

			super(mc);
			
			trace("--> instance de RadioCheckQuestion");
			_containerReponses = this.mc;
		}

		override public function get containerReponses():MovieClip
		{
			return _containerReponses;
		}
		override public function set containerReponses(clip:MovieClip)
		{
			_containerReponses = clip;
		}
		public function init(xmlConfig:XML)
		{
			super._init(xmlConfig);
			
			// init radio
			for (var j:int = 0; j < xmlConfig.children().length(); j++) {

				var clipReponse:MovieClip = _containerReponses.getChildByName("rep_" + xmlConfig.children()[j].@id) as MovieClip;
								
				
				trace("clipReponse " + clipReponse);
				
				// ajout réponse
				var node:XML = xmlConfig.children()[j];
				var rep:ReponseRadioCheck = new ReponseRadioCheck(clipReponse);
						
				rep.value = node.@value;
				rep.type = type;
				rep.id = node.@id;
				
				trace("rep.type " + rep.type);
				
				
				if (node.@zoneText == "false") {
					rep.clipMouseDown = rep.mc.box;
				}

				addReponse(rep);
				
				if (node.@selected != null && node.@selected == "true") {
					select(rep);
				}
				
				//_containerReponses.addChild(rep);
				
			}
						
		}
		override public function reset():void 
		{
			
			hideError();
			
			for (var i:int = 0; i < _tabReponses.length; i++) 
			{
				var rep:ReponseRadioCheck = _tabReponses[i];
				rep.selected = false;
				rep.mc.gotoAndPlay("out");
			}
			_reponse = null;
			
		}
		
		private function onFocusIn(e:FocusEvent):void 
		{
			_itemFocus = e.currentTarget as ReponseRadioCheck;
			_itemFocus.mc.stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
		}
		private function onFocusOut(e:FocusEvent):void 
		{
			_itemFocus.mc.stage.removeEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
			_itemFocus = null;
			
		}
		private function _onKeyDown(e:KeyboardEvent):void 
		{
			
			trace("e.keyCode " + e.keyCode);
			if ( _itemFocus != null && (e.keyCode == Keyboard.RIGHT || e.keyCode == Keyboard.DOWN || e.keyCode == Keyboard.SPACE))
			{
				if ( !_itemFocus.selected) {
					select(_itemFocus);
				}else {
					deselect(_itemFocus);
				}
				
			}
			
			
		}
		
		
		//--------------------------------------------------------
		override public function addReponse(itemRep:AbstractReponse)
		{

			itemRep.selected = false;
			
			itemRep.clipMouseDown.buttonMode = true;
			itemRep.clipMouseDown.addEventListener(MouseEvent.MOUSE_DOWN, onClickReponse);

			itemRep.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
			itemRep.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
			
			
			super.addReponse(itemRep);
		}
		
		//--------------------------------------------------------
		public function select(rep:AbstractReponse)
		{
			
			hideError();
			var type:String = rep.type;
			
			trace("rep.type " + rep.type);
			trace("rep.id " + rep.id);
			
			if (rep.selected == true)
			{
				switch (type)
				{
					case "checkBox":
						trace("out");
						rep.selected = false;
						rep.mc.gotoAndPlay("out");
						break;
					default: 
						break;
				}
				
			}else
			{	
				switch (type) 
				{
					case "radioButton":
						var i:int = 0;
						var clip:MovieClip;
						
						for (i=0; i < reponsesTab.length;i++ )
						{
							clip = (reponsesTab[i] as MovieClip);
							if (clip.selected == true && clip.type != "checkBox" )
							{
								clip.selected = false;
								clip.mc.gotoAndPlay("out");
							}

						}
						break;
						
					case "checkBox":
						for (i=0; i < reponsesTab.length;i++ )
						{
							clip = reponsesTab[i] as MovieClip;
							if (clip.selected == true && clip.type == "init" )
							{
								clip.selected = false;
								clip.mc.gotoAndPlay("out");
							}

						}
						break;
					
					case "init":
						for (i=0; i < reponsesTab.length;i++ )
						{
							if ((reponsesTab[i] as Object).selected == true)
							{
								(reponsesTab[i] as MovieClip).selected = false;
								(reponsesTab[i] as MovieClip).mc.gotoAndPlay("out");
							}

						}
						break;
					default: 
						break;
				}

				rep.selected = true;
				rep.mc.gotoAndPlay("select");
				
				_reponse = rep;
			}
		}


		override public function get reponse():AbstractReponse
		{ 
			return _reponse;
			
		}
		public function getItem(id:String):ReponseRadioCheck
		{

			for (var i:int = 0; i < _tabReponses.length; i++) 
			{
				var rep:ReponseRadioCheck = _tabReponses[i];
				if (  rep.id == id) {
					return rep;
				}
			}
			
			return null;
			
		}
		
		public function get reponsesSelected():Array
		{ 
			var tab:Array = new Array();
			for (var i:int = 0; i < _tabReponses.length; i++) 
			{
				var rep:AbstractReponse = _tabReponses[i];
				if ( rep.selected) {
					tab.push(rep);
				}
			}
			
			return tab;
			
		}

		//--------------------------------------------------------
		public function deselect(rep:AbstractReponse)
		{
			rep.selected = false;
			rep.mc.gotoAndPlay("out");
			
		}
		//--------------------------------------------------------
		public function onClickReponse(evt:MouseEvent)
		{
			if (evt.currentTarget is AbstractReponse) {
				select(evt.currentTarget as AbstractReponse);
			}else {
				if ( (evt.currentTarget as MovieClip).abstractReponse != null) {
					select((evt.currentTarget as MovieClip).abstractReponse);
				}
			}

		}
		
		
		
	}
}